﻿using UnityEngine;
using System.Collections;

public class CollisionDestination : MonoBehaviour 
{
	public float forceValue = 0;

	void OnCollisionEnter(Collision collision) 
	{
//		foreach (ContactPoint contact in collision.contacts) 
//		{
//			Debug.DrawRay(contact.point, contact.normal, Color.red,1);//可视化接触点
//			Debug.Log (contact.point);
//		}
		AddForceTo (collision.gameObject, forceValue, collision.contacts[0].point);
		if (collision.relativeVelocity.magnitude > 2)
		{
			//audio.Play();
			//Debug.Log ("// 如果碰撞体有较大冲击就播放声音");
		}	
	}

	private void AddForceTo(GameObject go, float force, Vector3 position)
	{
		Vector3 direction = go.transform.position - transform.position;
		go.GetComponent<Rigidbody> ().AddForceAtPosition (force * direction.normalized, position);
		//Debug.Log ("收到碰撞力");
	}
}
